#region File Header
//
// Program.cs - Program entry point
//
// Copyright (C) Javier Valcarce. BSD License
#endregion

#region Using Statements
using System;
using System.Diagnostics;
using System.Threading;
#endregion

namespace SwModem
{
    //
    // HOW TO TEST THIS PROGRAM
    // Install com0com serial port emulator and create a new serial port, for example COM8. Run 
    // HyperTerminal and connect it to this new serial port, the output modulated signal will be 
    // stored in output_xxx_xxx.wav file (it's not a standar WAV format: mono, 24-bit, play it 
    // with VideoLAN or Adobe Audition)
    //
    public static class Program
    {
        static bool exiting = false;

        static void Main(string[] args)
        {
            // debug traces to console output
            Debug.Listeners.Add(new ConsoleTraceListener());

            // CTRL+C on terminal is treated as an interrupt instead of as input.
            Console.TreatControlCAsInput = false;
            Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
            
            // let's build the graph
            Builder.Build();

            // start all dsp blocks
            //foreach (ConcurrentNode n in Builder.TheGraph)
            //    n.Control(NodeExeCtl.Start);

            // monitor its functioning
            while (!exiting)
            {
                PrintInfo();
                Thread.Sleep(500);            
            }

            //// stop all dsp blocks
            //foreach (Node n in Builder.TheGraph)
            //    n.Control(NodeExeCtl.Stop);
            
            //// Wait to finish all thread in each node
            //foreach (Node n in Builder.TheGraph)
            //{
            //    n.Control(NodeExeCtl.Abort);
            //    foreach (Thread t in n.Threads)
            //        while (!t.Join(1000))
            //            Console.WriteLine("Ops. A thread insists in not to die");
            //}


            PrintInfo();
            Console.WriteLine("Bye.");
        }


        private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs args)
        {            
            // Do not finish the process now, I will terminate it later (before, a soft shut 
            // down is required, free resources, close files, etc)
            args.Cancel = true;
            exiting = true;
        }

        private static void PrintInfo()
        {
            // Print on screen some information about the graph nodes status
            Console.Clear();
            Console.WriteLine(
                System.Windows.Forms.Application.ProductName    + " " +
                System.Windows.Forms.Application.ProductVersion + " " +
                DateTime.Now.ToString());

            Console.WriteLine("Executing graph... Press <C-c> to exit.");
            Console.WriteLine("");

            //foreach (rrentNode n in Builder.TheGraph)
            //{
            //    Console.WriteLine("Node " + n.Name + " [" + n.Status.ToString() + "]");
            //    foreach (Thread t in n.Threads)
            //    {
            //        Console.WriteLine("    " + t.Name + "[" + t.ManagedThreadId + "]");
            //    }
            //}                

        }



    }
}
